<template>
  <div class="container">
    <input type="text" placeholder="请输入想要搜索的内容" @input="searchAction($event)">
    <div v-if="searchResult.length > 0">
      <div
        v-for="(item,index) in searchResult"
        :key="index"
        class="hotArtcle"
        @click="gotoContent(item.id)"
      >
        <div class="title">{{ item.title }}</div>
        <div class="likenums">
          <img src="./images/addlike.png" alt="">
          {{ item.likenums }}
        </div>
        <div class="time">发布日期:{{ item.time }}</div>
        <div class="author">作者:{{ item.author }}</div>
        <div class="text">{{ item.content }}</div>
      </div>
    </div>
    <div v-else>
      <div
        v-for="(item,index) in hostlistinfo"
        :key="index"
        class="hotArtcle"
        @click="gotoContent(item.id)"
      >
        <div class="title">{{ item.title }}</div>
        <div class="likenums">
          <img src="./images/addlike.png" alt="">
          {{ item.likenums }}
        </div>
        <div class="time">发布日期:{{ item.time }}</div>
        <div class="author">作者:{{ item.author }}</div>
        <div class="text">{{ item.content }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { throttle } from '../libs/utils'
export default {
  data() {
    return {
      searchResult:[],
      hostlistinfo:[]
    }
  },
  created() {
    this.getHotList()
  },
  methods: {
    searchAction:throttle(function(e) {
      const inputValue = e.target.value
      this.searchResult = this.hostlistinfo.filter(item => {
        if(item.title.includes(inputValue)) {
          return item
        }
      })
    },1000),
    async getHotList() {
      const res = await this.$api.artcleList.getHotList()
      const { data: { data: hostlistinfo } } = res
      this.hostlistinfo = [
        ...hostlistinfo
      ]
    },
    async gotoContent(id) {
      const res = await this.$api.artcleList.getArtcledetails(id)
      const { data:{ data:artcledetailsinfo } } = res
      this.artcledetailsinfo = {
        ...artcledetailsinfo
      }
      this.$router.push({ path:'/content/', query:artcledetailsinfo })
      // console.log(this.artcledetailsinfo,'----artcledetailsinfo')
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px 300px 0 300px;
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
  input {
    margin-bottom: 30px;
    width: 500px;
  }
  .hotArtcle {
    position: relative;
    width: 1000px;
    height: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    flex-direction: column;
    cursor: pointer;
    background-color: #ffff;
    box-shadow: 10px 10px 5px #888888;
    margin-bottom: 20px;
    .title {
      margin-top: 10px;
      margin-left: 10px;
      font-size: 20px;
      color: #fb7299;
      font-weight: 600;
      margin-bottom: 5px;
    }
    .likenums {
      position: absolute;
      top: 10px;
      right: 500px;
      display: flex;
      line-height: 30px;
      img {
        width: 30px;
        height: 30px;
      }
    }
    .time {
      position: absolute;
      top: 10px;
      right: 10px;
      color: #99a2aa;
      font-size: 14px;
    }
    .author {
      font-size: 12px;
      color: #000;
      margin-left: 10px;
      margin-bottom: 15px;
    }
    .text {
      margin-left: 10px;
      margin-right: 10px;
      font-size: 16px;
      color: #000;
    }
  }
}
</style>

